package com.supply.service.impl;

import com.supply.global.Constants;
import com.supply.mapper.GoodsMapper;
import com.supply.mapper.LeaveBillMapper;
import com.supply.mapper.OutBoundBillMapper;
import com.supply.mapper.SalesBillMapper;
import com.supply.pojo.Goods;
import com.supply.pojo.OutBoundBill;
import com.supply.pojo.SalesBill;
import com.supply.req.OutBoundBillGroup;
import com.supply.service.OutBoundProcessService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class OutBoundProcessServiceImpl implements OutBoundProcessService {
    @Autowired
    private OutBoundBillMapper outBoundBillMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SalesBillMapper salesBillMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Override
    public int startOutBound(OutBoundBillGroup outBoundBillGroup) {
        //插入出库单数据，并获得回填Id
        int insertRs = outBoundBillMapper.insert(outBoundBillGroup);
        System.out.println("插入出库单回填的id：" + outBoundBillGroup.getId());

        String key = Constants.OUTBOUND_KEY;
        //构建businessKey，该字段可以记录流程实例关联的出库单的Id，并且该字段是唯一标识
        String businessKey = key + "." + outBoundBillGroup.getId();
        //设置该流程的审核人
        Map<String,Object> map = new HashMap<>();
        map.put("materialman",outBoundBillGroup.getUserId().toString());
        map.put("buyer",outBoundBillGroup.getBuyer());
        map.put("salesman",outBoundBillGroup.getSalesman());
        map.put("manger",outBoundBillGroup.getManger());
        //开启流程
        runtimeService.startProcessInstanceByKey(key,businessKey,map);

        //出库单开启申请，商品处于预出库状态，库存数量减少，更新库存
        Goods goods = goodsMapper.selectByPrimaryKey(outBoundBillGroup.getGoodsId());
        goods.setQuantity(goods.getQuantity() - outBoundBillGroup.getQuantity());
        goodsMapper.updateByPrimaryKey(goods);

        //若该出库单关联有销售单Id，则将关联的销售单设置为“等待出库审批”状态
        if (outBoundBillGroup.getSalesBillId() != null) {
            SalesBill salesBill = salesBillMapper.selectByPrimaryKey(outBoundBillGroup.getSalesBillId());
            salesBill.setState(1);
            salesBillMapper.updateByPrimaryKey(salesBill);
        }

        return insertRs;
    }

    @Override
    public OutBoundBill queryOutBoundBillByTaskId(String taskId) {
        //根据taskId查询task任务
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        //根据从task任务获取的processInstanceId获取流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(task.getProcessInstanceId())
                .singleResult();
        //从流程实例中获取businessKey，进而获取出库单Id
        String businessKey = processInstance.getBusinessKey();
        String id = businessKey.split("\\.")[1];
        return outBoundBillMapper.selectByPrimaryKey(Long.parseLong(id));
    }
}
